//
//  BN_UseAgreementView.m
//  TestAgreement
//
//  Created by 刘泽琛 on 14-7-18.
//  Copyright (c) 2014年 刘 泽琛. All rights reserved.
//
#define MAIN_VERSION [[[[UIDevice currentDevice]systemVersion]componentsSeparatedByString:@"."][0] integerValue]
#define TITLE_FONT 18
#define PART_TITLE_FONT 17
#define CONTENT_FONT 16
static int height = 0;

#import "BN_UseAgreementView.h"

@implementation BN_UseAgreementView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // code...
    }
    return self;
}

- (void)initData {
    NSString *path = [[NSBundle mainBundle]pathForResource:@"UserAgreementPlist" ofType:@"plist"];
    _plistArray = [[NSArray alloc]initWithContentsOfFile:path];
}

#pragma mark
#pragma mark 文本高度自适应
// 封装自动撑开高度的方法
- (CGSize)getTextSize:(CGSize)provisionsSize disFont:(UIFont *)font contentStr:(NSString *)contentStr contextDic:(NSDictionary *)contextDic {
    
    CGSize size;
    switch (MAIN_VERSION) {
        case 6:
            size = [contentStr sizeWithFont:font constrainedToSize:provisionsSize lineBreakMode:NSLineBreakByWordWrapping];
            break;
        default:
            
           size = [contentStr boundingRectWithSize:provisionsSize options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine attributes:contextDic context:nil].size;
            break;
    }
    return size;
}

/*
- (void)drawRect:(CGRect)rect
{
    [self initData];
    for (int i = 0; i < _plistArray.count; i++) {
        
        switch (i) {
            case 0:
                _rang_numb = 11;
                break;
            case 1:
                _rang_numb = 8;
                break;
            case 2:
                _rang_numb = 8;
                break;
            case 3:
                _rang_numb = 8;
                break;
            case 4:
                _rang_numb = 6;
                break;
            case 5:
                _rang_numb = 6;
                break;
            case 6:
                _rang_numb = 6;
                break;
            case 7:
                _rang_numb = 6;
                break;
            default:
                break;
        }
        
        NSString *resultStr = _plistArray[i];
        NSInteger otherPart = resultStr.length-_rang_numb;
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:resultStr];
        
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,_rang_numb)];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(_rang_numb,otherPart)];
        
        if (i == 0) {
            [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:TITLE_FONT] range:NSMakeRange(0, _rang_numb)];
        }else {
            [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:PART_TITLE_FONT] range:NSMakeRange(0, _rang_numb)];
        }
        [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:CONTENT_FONT] range:NSMakeRange(_rang_numb,otherPart)];
        
        UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        contentLabel.tag = i+1;
        contentLabel.attributedText = str;
        [self addSubview:contentLabel];
        
        contentLabel.numberOfLines = 0;
        [contentLabel sizeToFit];
        
        if (i == 0) {
            height = 20;
            contentLabel.textAlignment = NSTextAlignmentCenter;
        }else {
            UILabel *otherLabel = (UILabel *)[self viewWithTag:i];
            height = CGRectGetMaxY(otherLabel.frame);
        }
        
        UIFont * tfont = [UIFont systemFontOfSize:CONTENT_FONT];
        CGSize size = CGSizeMake(300,10000);
        NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
        CGSize actualsize = [self getTextSize:size disFont:tfont contentStr:_plistArray[i] contextDic:tdic];
        
        contentLabel.frame = CGRectMake(15, height+20, 300, actualsize.height);
        self.contentSize = CGSizeMake(300, CGRectGetMaxY(contentLabel.frame)+20);
    }
}*/

- (void)loadHintView {
    [self initData];
    for (int i = 0; i < _plistArray.count; i++) {
        
        switch (i) {
            case 0:
                _rang_numb = 11;
                break;
            case 1:
                _rang_numb = 8;
                break;
            case 2:
                _rang_numb = 8;
                break;
            case 3:
                _rang_numb = 8;
                break;
            case 4:
                _rang_numb = 6;
                break;
            case 5:
                _rang_numb = 6;
                break;
            case 6:
                _rang_numb = 6;
                break;
            case 7:
                _rang_numb = 6;
                break;
            default:
                break;
        }
        
        NSString *resultStr = _plistArray[i];
        NSInteger otherPart = resultStr.length-_rang_numb;
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:resultStr];
        
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,_rang_numb)];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(_rang_numb,otherPart)];
        
        if (i == 0) {
            [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:TITLE_FONT] range:NSMakeRange(0, _rang_numb)];
        }else {
            [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:PART_TITLE_FONT] range:NSMakeRange(0, _rang_numb)];
        }
        [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:CONTENT_FONT] range:NSMakeRange(_rang_numb,otherPart)];
        
        UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        contentLabel.tag = i+1;
        contentLabel.attributedText = str;
        [self addSubview:contentLabel];
        
        contentLabel.numberOfLines = 0;
        [contentLabel sizeToFit];
        
        if (i == 0) {
            height = 20;
            contentLabel.textAlignment = NSTextAlignmentCenter;
        }else {
            UILabel *otherLabel = (UILabel *)[self viewWithTag:i];
            height = CGRectGetMaxY(otherLabel.frame);
        }
        
        UIFont * tfont = [UIFont systemFontOfSize:CONTENT_FONT];
        CGSize size = CGSizeMake(300,10000);
        NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
        CGSize actualsize = [self getTextSize:size disFont:tfont contentStr:_plistArray[i] contextDic:tdic];
        
        contentLabel.frame = CGRectMake(15, height+20, 300, actualsize.height);
        self.contentSize = CGSizeMake(300, CGRectGetMaxY(contentLabel.frame)+20);
    }
}

- (void)popHintView {
    [self initData];
    for (int i = 0; i < _plistArray.count; i++) {
        
        switch (i) {
            case 0:
                _rang_numb = 11;
                break;
            case 1:
                _rang_numb = 8;
                break;
            case 2:
                _rang_numb = 8;
                break;
            case 3:
                _rang_numb = 8;
                break;
            case 4:
                _rang_numb = 6;
                break;
            case 5:
                _rang_numb = 6;
                break;
            case 6:
                _rang_numb = 6;
                break;
            case 7:
                _rang_numb = 6;
                break;
            default:
                break;
        }
        
        NSString *resultStr = _plistArray[i];
        NSInteger otherPart = resultStr.length-_rang_numb;
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:resultStr];
        
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,_rang_numb)];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(_rang_numb,otherPart)];
        
        if (i == 0) {
            [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:TITLE_FONT] range:NSMakeRange(0, _rang_numb)];
        }else {
            [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:PART_TITLE_FONT] range:NSMakeRange(0, _rang_numb)];
        }
        [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:CONTENT_FONT] range:NSMakeRange(_rang_numb,otherPart)];
        
        UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        contentLabel.tag = i+1;
        contentLabel.attributedText = str;
        [self addSubview:contentLabel];
        
        contentLabel.numberOfLines = 0;
        [contentLabel sizeToFit];
        
        if (i == 0) {
            height = 20;
            contentLabel.textAlignment = NSTextAlignmentCenter;
        }else {
            UILabel *otherLabel = (UILabel *)[self viewWithTag:i];
            height = CGRectGetMaxY(otherLabel.frame);
        }
        
        UIFont * tfont = [UIFont systemFontOfSize:CONTENT_FONT];
        CGSize size = CGSizeMake(SCREEN_WIDTH-20*2,10000);
        NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
        CGSize actualsize = [self getTextSize:size disFont:tfont contentStr:_plistArray[i] contextDic:tdic];
        
        contentLabel.frame = CGRectMake(15, height+20, SCREEN_WIDTH-20*3, actualsize.height);
        
        if (MAIN_VERSION > 6) {
            self.contentSize = CGSizeMake(SCREEN_WIDTH-20*2, CGRectGetMaxY(contentLabel.frame)+20);
        }else {
            self.contentSize = CGSizeMake(SCREEN_WIDTH-20*2, CGRectGetMaxY(contentLabel.frame)+20+64);
        }
    }
}


@end
